package com.loyal.loyalerp.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loyal.loyalerp.humenresource.askleave.model.vo.Check;
import com.loyal.loyalerp.mapper.KaoQinMapper;
import com.loyal.loyalerp.model.ChangeInitData;
import com.loyal.loyalerp.model.KaoQinMessage;
import com.loyal.loyalerp.model.RsCheck;
import com.loyal.loyalerp.model.vo.CompanyIdAndYears;
import com.loyal.loyalerp.model.vo.ExcelVo;
import com.loyal.loyalerp.model.vo.PersonBaseData;
import com.loyal.loyalerp.model.vo.PersonIdAndYear;
import com.loyal.loyalerp.model.vo.UsernameAndWork;
import com.loyal.loyalerp.utils.WorkDayMath;

@Service
public class KaoQinServiceImpl {
	@Autowired
	private KaoQinMapper mapper;

	// 判断当前年份有没有被定义过
	public int selectYearHasInit(int year, int companyId) {
		CompanyIdAndYears com = new CompanyIdAndYears();
		com.setCompanyid(companyId);
		com.setYears(year);
		return mapper.selectYearHasInit(com);
	}

	// 判断该公司是否有员工
	public int hasPerson(int companyId) {
		return mapper.isHasPerson(companyId);
	}

	public void initData(String years, int companyid) throws ParseException {
		// 查询当前所有person员工的信息
		List<PersonBaseData> data = mapper.selectAllPerson(companyid);
		// 计算每个月上班天数
		int workDayMonth[] = WorkDayMath.importDate(years);
		// 计算年度上班总数期初数
		int workDaysTotal = 0;
		for (int monthWorkDay : workDayMonth) {
			workDaysTotal = workDaysTotal + monthWorkDay;
		}
		// 初始化年假表
		for (PersonBaseData person : data) {
			Date officeDate = person.getOfficeDate();
			// 判断当前用户有没有入职超过一年,如果没有超过一年，按照比例折算假期
			Date date = new Date();
			int officeYear = officeDate.getYear();
			// 当前的年份
			Date nowDate = new Date();
			// 定义放假天数
			float Holidays = 0;
			//定义本年的剩余年假
			float HolidaysEnd = 0;
			int nowYear = Integer.parseInt(years) - 1900;
			// 当年入职
			if (nowYear - officeYear == 0) {
				// 放假天数
				Holidays = WorkDayMath.mathYearDays(officeDate, years) * 5;
			}
			// 入职满一年
			else if (nowYear - officeYear > 0) {
				// 放假天数
				Holidays = 5 + (nowYear - officeYear);
			}
			float holidaysStart;
			// 判断员工是否有上年有剩余年假
			int count = HasLastHolidays(person.getPersonId(), Integer.parseInt(years) - 1);
			if (count == 0) {
				holidaysStart = 0.000f;
				HolidaysEnd = holidaysStart + Holidays;
			} else {
				// 获取上年假期剩余,默认还剩0天
				holidaysStart = getLastHolidays(person.getPersonId(), Integer.parseInt(years) - 1);
				// 本年的剩余假期
				HolidaysEnd = holidaysStart + Holidays;
			}
			KaoQinMessage msg = new KaoQinMessage();
			msg.setKjYear(Integer.parseInt(years));
			msg.setCompanyId(person.getCompanyId());
			msg.setCompanyName(person.getCompanyName());
			msg.setPersonName(person.getPersonName());
			msg.setPersonId(person.getPersonId());
			msg.setHolidays(Holidays);
			msg.setHolidaysStart(holidaysStart);
			msg.setOverTimeDaysStart(0);
			msg.setWorkDays01(workDayMonth[0]);
			msg.setOffDays01(0);
			msg.setSalaryDays01(0);
			msg.setWorkDays02(workDayMonth[1]);
			msg.setOffDays02(0);
			msg.setSalaryDays02(0);
			msg.setWorkDays03(workDayMonth[2]);
			msg.setOffDays03(0);
			msg.setSalaryDays03(0);
			msg.setWorkDays04(workDayMonth[3]);
			msg.setOffDays04(0);
			msg.setSalaryDays04(0);
			msg.setWorkDays05(workDayMonth[4]);
			msg.setOffDays05(0);
			msg.setSalaryDays05(0);
			msg.setWorkDays06(workDayMonth[5]);
			msg.setOffDays06(0);
			msg.setSalaryDays06(0);
			msg.setWorkDays07(workDayMonth[6]);
			msg.setOffDays07(0);
			msg.setSalaryDays07(0);
			msg.setWorkDays08(workDayMonth[7]);
			msg.setOffDays08(0);
			msg.setSalaryDays08(0);
			msg.setWorkDays09(workDayMonth[8]);
			msg.setOffDays09(0);
			msg.setSalaryDays09(0);
			msg.setWorkDays10(workDayMonth[9]);
			msg.setOffDays10(0);
			msg.setSalaryDays10(0);
			msg.setWorkDays11(workDayMonth[10]);
			msg.setOffDays11(0);
			msg.setSalaryDays11(0);
			msg.setWorkDays12(workDayMonth[11]);
			msg.setOffDays12(0);
			msg.setSalaryDays12(0);
			msg.setWorkDaysTotal(workDaysTotal);
			msg.setOffDaysTotal(0);
			msg.setSalaryDaysTotal(workDaysTotal);
			msg.setHolidaysTotal(0);
			msg.setHolidaysEnd(HolidaysEnd);
			msg.setOverTimeDaysTotal(0);
			msg.setLawDaysTotal(0);
			msg.setSickDaysTotal(0);
			msg.setPersonDaysTotal(0);
			msg.setKpi01(0);
			msg.setKpi02(0);
			msg.setKpi03(0);
			msg.setKpi04(0);
			msg.setKpi05(0);
			msg.setKpi06(0);
			msg.setKpi07(0);
			msg.setKpi08(0);
			msg.setKpi09(0);
			msg.setKpi10(0);
			msg.setKpi11(0);
			msg.setKpi12(0);
			msg.setRemark1(null);
			msg.setRemark2(null);
			msg.setRemark3(null);
			mapper.insertPersonInit(msg);

		}

	}

	// 查看所有员工期初信息
	public List<KaoQinMessage> selectAllPersonInitData(String flag) {
		return mapper.selectAllPersonInitData(flag);
	}

	// 搜索员工考勤信息
	public List<KaoQinMessage> selectPersonKaoQinByName(UsernameAndWork username) {
		return mapper.selectPersonKaoQinByName(username);
	}

	// 获取员工上一年的剩余年假
	public float getLastHolidays(int personId, int year) {
		PersonIdAndYear model = new PersonIdAndYear();
		model.setPersonId(personId);
		model.setYear(year);
		return mapper.selectPersonHasLastHoliday(model);
	}

	// 获取员工是否上一年的剩余年假
	public int HasLastHolidays(int personId, int year) {
		PersonIdAndYear model = new PersonIdAndYear();
		model.setPersonId(personId);
		model.setYear(year);
		return mapper.PersonIsExistLastHoliday(model);
	}

	// 更新期初数据
	public void updateInitData(ChangeInitData data) {
		KaoQinMessage msg = mapper.selectInitDataByPrimaryKey(data.getId());
		data.setHolidaysEnd(data.getHolidaysStart()+msg.getHolidays());
		mapper.updateInitData(data);
	}

	// 往审核表中添加公司信息
	public void insertCompanyCheck(RsCheck check) {
		mapper.insertCompanyCheck(check);
	}

	// 判断能不能审核
	public int selectAbandonCheck(CompanyIdAndYears cmd) {
		return mapper.selectAbandonCheck(cmd);
	}

	// 判断能不能审核
	public int selectIsCheck(CompanyIdAndYears cmd) {
		return mapper.selectInitDataCheck(cmd);
	}

	// 审核
	public void updateAbandonCheck(CompanyIdAndYears year) {
		mapper.updateAbandonCheck(year);
	}

	// 放弃审
	public void updateIsCheck(CompanyIdAndYears year) {
		mapper.updateIsCheck(year);
	}

	// 根据公司id跟年份查询期初信息
	public List<KaoQinMessage> selectInitDataByCompanyIdAndYears(CompanyIdAndYears year) {
		return mapper.selectInitDataByCompanyIdAndYears(year);
	}

	// 恢复审核
	public void updateIsNoCheck(CompanyIdAndYears year) {
        mapper.updateIsNoCheck(year);
	}
	
	//删除所有考勤信息
	public void deleteAllRecord() {
		mapper.deleteAllRecord();
	}
	
	public List<KaoQinMessage> selectAllPersonInitDataByExcel(ExcelVo vo){
	    System.out.println("的实例卷发");
		return mapper.selectAllPersonInitDataByExcel(vo);
	}
	
	public int selectPersonIdById(int id) {
		return mapper.selectPersonIdById(id);
	}
	
}
